﻿<?php
include_once("../function.php");
//print_r($_GET);die;//Array ( [addr_id] => 6 [color] => #004080,#004080,#004080,#004080 [uid] => 4 [pid] => 162,161,160,161 [total] => 20044 [size] => XL,XL,XL,XL, [sub] => 提交订单 )
//print_r($_GET);die;//Array ( [addr_id] => 7 [color] => #0080ff [uid] => 4 [pid] => 160 [type] => 1 [price] => 3199 [size] => L [num] => 1 [sub] => 提交订单 )
extract($_GET);//$addr_id $uid $pid $total/$price $size $color $type $num
$addr_id=trim($addr_id);
$uid=trim($uid);
$type=trim($type);
//date_default_timezone_set("PRC");
$time=time();
$ordernum=date("Ymd").rand(1000,9999);

//判断是否有库存
$sql="select * from ts_product where pid=$pid";
$res=mysqli_query($link,$sql);
if(is_object($res))
{
	if(mysqli_num_rows($res)==1)
	{
		$one=mysqli_fetch_assoc($res);
		{
			if($one['snums']<1)
			{
				msg("该商品没有库存，请另选商品!","./index.php");exit;
			}
		}
	}
}
if($type==2)
{
	$pid=trim($pid);
	$total=trim($total);
	$size=trim($size);
	$color=trim($color);
	$sql="insert into ts_order(addr_id,user_id,goods_id,subtotal,order_time,order_sn) values($addr_id,$uid,'$pid',$total,'$time','$ordernum')";
	//echo $sql;
	//var_dump(mysqli_query($link,$sql));die;
	$res=mysqli_query($link,$sql);
	if($res)
	{
		if(mysqli_affected_rows($link)==1)
		{
			//header("location:./done.php?total=$total");
		}
	}
	//获取订单主键
	$oid=mysqli_insert_id($link);
	//echo $oid;die;

	//遍历购物车
	$sql="select * from ts_cart where user_id=$uid";
	$res=mysqli_query($link,$sql);
	if(is_object($res))
	{
		if(mysqli_num_rows($res)>0)
		{
			$arr=array();
			while(($list=mysqli_fetch_assoc($res))!=false)
			{
				$arr[]=$list;
			}
		}
	}

	if(isset($arr))
	{
		foreach($arr as $v)
		{
			$sql="insert into ts_order_goods(oid,uid,pid,color,size,num) values($oid,'$uid','$v[pid]','$v[color]','$v[size]','$v[num]')";
			$res=mysqli_query($link,$sql);
			if($res)
			{
				if(mysqli_affected_rows($link)==1)
				{
					
				}
			}
		}
	}
	//更新库存
	$sql="select * from ts_order_goods where uid=$uid";
	$res=mysqli_query($link,$sql);
	if(is_object($res))
	{
		if(mysqli_num_rows($res)>0)
		{
			$goods=array();
			while(($list=mysqli_fetch_assoc($res))!=false)
			{
				$goods[]=$list;
			}
		}
	}
	if(isset($goods))
	{
		foreach($goods as $j)
		{
			//$good=$j['pid'];
			$sql="select * from ts_product where pid=$j[pid]";
			$res=mysqli_query($link,$sql);
			if(is_object($res))
			{
				if(mysqli_num_rows($res)>0)
				{
					//$goods=array();
					$one2=mysqli_fetch_assoc($res);
					$num=$one2['snums'];
				}
			}
			$num=$num-$j['num'];
			if($num<0)
			{
				$sql="update ts_product set snums=0 where pid=$j[pid]";
				//echo $sql;
				//var_dump(mysqli_query($link,$sql));die;
				$res=mysqli_query($link,$sql);
				if($res)
				{
					$total=$total;
					$pid=$pid;
						header("location:./done.php?total=$total&pid=$pid&type=2&uid=$uid&oid=$oid");exit;
				}
			}
			else
			{
				$sql="update ts_product set snums=$num where pid=$j[pid]";
				//echo $sql;
				//var_dump(mysqli_query($link,$sql));die;
				$res=mysqli_query($link,$sql);
				if($res)
				{
					$total=$total;
					$pid=$pid;
						header("location:./done.php?total=$total&pid=$pid&type=2&uid=$uid&oid=$oid");exit;
				}
			}
		}
	}
}
elseif($type==1)
{
	$price=$price*$num;
	$pid=trim($pid);
	$color=trim($color);
	$size=trim($size);
	$num=trim($num);
	$sql="insert into ts_cart(color,size,num,user_id,pid) values('$color','$size','$num','$uid','$pid')";
	$res=mysqli_query($link,$sql);
	if($res)
	{
		if(mysqli_affected_rows($link)==1)
		{
			//header("location:./done.php?total=$total");
		}
	}
	
	$sql="insert into ts_order(addr_id,user_id,goods_id,subtotal,order_time,order_sn) values('$addr_id','$uid','$pid','$price','$time','$ordernum')";
	$res=mysqli_query($link,$sql);
	if($res)
	{
		if(mysqli_affected_rows($link)==1)
		{
			//header("location:./done.php?total=$total");
		}
	}

	//获取订单主键
	$oid=mysqli_insert_id($link);
	//echo $oid;die;
	$sql="insert into ts_order_goods(oid,uid,pid,color,size,num) values($oid,'$uid','$pid','$color','$size','$num')";
	$res=mysqli_query($link,$sql);
	if($res)
	{
		if(mysqli_affected_rows($link)==1)
		{			
		}
	}
	//遍历购物车
	$sql="select * from ts_cart where user_id=$uid";
	$res=mysqli_query($link,$sql);
	if(is_object($res))
	{
		if(mysqli_num_rows($res)>0)
		{
			$one=mysqli_fetch_assoc($res);
			$cartnum=$one['num'];
		}
	}
	//更新库存
	$sql="select * from ts_product where pid=$pid";
	$res=mysqli_query($link,$sql);
	if(is_object($res))
	{
		if(mysqli_num_rows($res)==1)
		{
			$one2=mysqli_fetch_assoc($res);
			$snums=$one2['snums'];
			
		}	
	}
	$snums=$snums-$cartnum;
	if($snums<0)
	{
		$sql="update ts_product set snums=0 where pid=$pid";
		//echo $sql;
		//var_dump(mysqli_query($link,$sql));die;
		$res=mysqli_query($link,$sql);
		if($res)
		{
			
			//$price=$price;
			//$pid=$pid;
			header("location:./done.php?price=$price&pid=$pid&type=1&uid=$uid&oid=$oid");exit;
		}
	}
	else
	{
		$sql="update ts_product set snums=$snums where pid=$pid";
		//echo $sql;
		//var_dump(mysqli_query($link,$sql));die;
		$res=mysqli_query($link,$sql);
		if($res)
		{
			
			//$price=$price;
			//$pid=$pid;
			header("location:./done.php?price=$price&pid=$pid&type=1&uid=$uid&oid=$oid");exit;
		}
	}
}
?>